package com.talent.service.iot.mqtt.server;

import com.talent.service.iot.mqtt.codec.TlMqttMessageCodec;
import com.talent.service.iot.mqtt.codec.decoder.*;
import com.talent.service.iot.mqtt.codec.encoder.*;
import com.talent.service.iot.mqtt.handler.*;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * @ClassName：TlMqttChannelInitializer
 * @Author: hszhou
 * @Date: 2024/12/9 16:14
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
public class TlMqttChannelInitializer extends ChannelInitializer {

    private final TlMqttConnectHandler mqttConnectHandler;

    private final TlMqttPublishHandler mqttPublishHandler;

    private final TlMqttSubScribeHandler mqttSubScribeHandler;

    private final TlMqttUnSubScribeHandler mqttUnSubScribeHandler;

    private final TlMqttHeartBeatHandler mqttHeartBeatHandler;

    private final TlMqttDisconnectHandler mqttDisconnectHandler;

    private final TlMqttPubCompHandler mqttPubCompHandler;

    private final TlMqttPubAckHandler mqttPubAckHandler;

    private final TlMqttPubRecHandler mqttPubRecHandler;

    private final TlMqttPubRelHandler mqttPubRelHandler;

    private final TlMqttConnackEncoder mqttConnackEncoder;

    private final TlMqttHeaderBeatEncoder mqttHeaderBeatEncoder;

    private final TlMqttSubAckEncoder mqttSubAckEncoder;

    private final TlMqttUnSubAckEncoder mqttUnSubAckEncoder;

    private final TlMqttPublishEncoder mqttPublishEncoder;

    private final TlMqttPubAckEncoder mqttPubAckEncoder;

    private final TlMqttPubRecEncoder mqttPubRecEncoder;
    private final TlMqttPubRelEncoder mqttPubRelEncoder;


    private final TlMqttPubCompEncoder mqttPubCompEncoder;

    private final TlMqttExceptionHandler mqttExceptionHandler;

    private final HeartbeatEventTriggered heartbeatEventTriggered;

    private final TlMqttConnectDecoder connectDecoder;

    private final TlMqttDisConnectDecoder disConnectDecoder;

    private final TlMqttHeartBeatDecoder heartBeatDecoder;

    private final TlMqttPubAckDecoder pubAckDecoder;

    private final TlMqttPubCompDecoder pubCompDecoder;

    private final TlMqttPublishDecoder publishDecoder;

    private final TlMqttPubRecDecoder pubRecDecoder;

    private final TlMqttPubRelDecoder pubRelDecoder;

    private final TlMqttSubscribeDecoder subscribeDecoder;

    private final TlMqttUnSubscribeDecoder unSubscribeDecoder;



    public TlMqttChannelInitializer(
            TlMqttConnectHandler mqttConnectHandler,
            TlMqttPublishHandler mqttPublishHandler,
            TlMqttSubScribeHandler mqttSubScribeHandler,
            TlMqttUnSubScribeHandler mqttUnSubScribeHandler,
            TlMqttHeartBeatHandler mqttHeartBeatHandler,
            TlMqttDisconnectHandler mqttDisconnectHandler,
            TlMqttPubCompHandler mqttPubCompHandler,
            TlMqttPubAckHandler mqttPubAckHandler,
            TlMqttPubRecHandler mqttPubRecHandler,
            TlMqttPubRelHandler mqttPubRelHandler,
            TlMqttConnackEncoder connackEncoder,
            TlMqttHeaderBeatEncoder mqttHeaderBeatEncoder,
            TlMqttSubAckEncoder mqttSubAckEncoder,
            TlMqttUnSubAckEncoder mqttUnSubAckEncoder,
            TlMqttPublishEncoder mqttPublishEncoder,
            TlMqttPubAckEncoder mqttPubAckEncoder,
            TlMqttPubRecEncoder mqttPubRecEncoder,
            TlMqttPubRelEncoder mqttPubRelEncoder,
            TlMqttPubCompEncoder mqttPubCompEncoder,
            TlMqttExceptionHandler mqttExceptionHandler,
            HeartbeatEventTriggered heartbeatEventTriggered,

            TlMqttConnectDecoder connectDecoder,
            TlMqttDisConnectDecoder disConnectDecoder,
            TlMqttHeartBeatDecoder heartBeatDecoder,
            TlMqttPubAckDecoder pubAckDecoder,
            TlMqttPubCompDecoder pubCompDecoder,
            TlMqttPublishDecoder publishDecoder,
            TlMqttPubRecDecoder pubRecDecoder, TlMqttPubRelDecoder pubRelDecoder, TlMqttSubscribeDecoder subscribeDecoder, TlMqttUnSubscribeDecoder unSubscribeDecoder
    ){
        this.mqttConnectHandler=mqttConnectHandler;
        this.mqttPublishHandler=mqttPublishHandler;
        this.mqttSubScribeHandler=mqttSubScribeHandler;
        this.mqttUnSubScribeHandler=mqttUnSubScribeHandler;
        this.mqttHeartBeatHandler=mqttHeartBeatHandler;
        this.mqttDisconnectHandler=mqttDisconnectHandler;
        this.mqttPubCompHandler=mqttPubCompHandler;
        this.mqttPubAckHandler=mqttPubAckHandler;
        this.mqttPubRecHandler=mqttPubRecHandler;
        this.mqttPubRelHandler=mqttPubRelHandler;
        this.mqttConnackEncoder=connackEncoder;
        this.mqttHeaderBeatEncoder=mqttHeaderBeatEncoder;
        this.mqttSubAckEncoder=mqttSubAckEncoder;
        this.mqttUnSubAckEncoder=mqttUnSubAckEncoder;
        this.mqttPublishEncoder=mqttPublishEncoder;
        this.mqttPubAckEncoder=mqttPubAckEncoder;
        this.mqttPubRecEncoder=mqttPubRecEncoder;
        this.mqttPubRelEncoder=mqttPubRelEncoder;
        this.mqttPubCompEncoder=mqttPubCompEncoder;
        this.mqttExceptionHandler=mqttExceptionHandler;
        this.heartbeatEventTriggered=heartbeatEventTriggered;

        this.connectDecoder = connectDecoder;
        this.disConnectDecoder = disConnectDecoder;
        this.heartBeatDecoder = heartBeatDecoder;
        this.pubAckDecoder = pubAckDecoder;
        this.pubCompDecoder = pubCompDecoder;
        this.publishDecoder = publishDecoder;
        this.pubRecDecoder = pubRecDecoder;
        this.pubRelDecoder = pubRelDecoder;
        this.subscribeDecoder = subscribeDecoder;
        this.unSubscribeDecoder = unSubscribeDecoder;
    }
    @Override
    protected void initChannel(Channel channel) throws Exception {

        ChannelPipeline pipeline = channel.pipeline();

        //协议解析
        pipeline.addLast(new TlMqttMessageCodec(connectDecoder,
                disConnectDecoder,heartBeatDecoder,pubAckDecoder,
                pubCompDecoder,publishDecoder,pubRecDecoder,
                pubRelDecoder,subscribeDecoder,unSubscribeDecoder))
       // pipeline.addLast(tlMqttMessageCodec)
        //连接处理
        .addLast(mqttConnectHandler)
        //publish消息接收处理器
        .addLast(mqttPublishHandler)
        //订阅主题处理
        .addLast(mqttSubScribeHandler)
        //取消订阅处理
        .addLast(mqttUnSubScribeHandler)
        //心跳检测处理
        .addLast(mqttHeartBeatHandler)
        //断开连接处理
        .addLast(mqttDisconnectHandler)
        //pubcomp处理
        .addLast(mqttPubCompHandler)
        //puback处理
        .addLast(mqttPubAckHandler)
        //rec处理
        .addLast(mqttPubRecHandler)
        //rel处理
        .addLast(mqttPubRelHandler)
        //ack编码
        .addLast(mqttConnackEncoder)
        //心跳恢复编码
        .addLast(mqttHeaderBeatEncoder)
        //订阅ack编码
        .addLast(mqttSubAckEncoder)
        //取消订阅ack编码
        .addLast(mqttUnSubAckEncoder)
        //publish编码
        .addLast(mqttPublishEncoder)
        //puback编码
        .addLast(mqttPubAckEncoder)
        //订阅ack编码
        //rec编码
        .addLast(mqttPubRecEncoder)
                .addLast(mqttPubRelEncoder)
        //comp编码
        .addLast(mqttPubCompEncoder)
        //异常处理
        .addLast(mqttExceptionHandler)
        //心跳检测
        .addLast(heartbeatEventTriggered)
        //心跳
        .addLast( "idleHandler", new IdleStateHandler(
                0, 0,  10));
    }
}
